<template>
  <a-modal
    :open="open"
    width="1100px"
    title="打印处方单"
    centered
    destroy-on-close
    :ok-button-props="{ disabled: checkedData.length <= 0 }"
    @cancel="onCancel"
    @ok="onOk"
  >
    <div class="overflow-y-auto min-h-[40vh] max-h-[80vh]">
      <div v-for="(item, index) in tableData" :key="index" class="flex-auto min-w-0 mb-4">
        <div class="flex justify-between mb-1">
          <div class="font-semibold">处方号：{{ item.cfbh }}</div>
          <div>
            <a-switch v-model:checked="item.checked" @change="onChangeCheck" />
          </div>
        </div>
        <BaseTable :data-source="item.children" :expanded-row-keys="item.expandedRowKeys" column-code="000541" row-key="id">
          <template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex === 'link'">
              <div class="table-link"></div>
            </template>
            <template v-if="column.dataIndex === 'yzzt'">
              <a-tag v-if="record.yzzt === 0" color="blue">新开立</a-tag>
              <a-tag v-if="record.yzzt === 1" color="green">已签署</a-tag>
              <a-tag v-if="record.yzzt === 2" color="red">已作废</a-tag>
            </template>
            <template v-if="column.dataIndex === 'sfxm'">
              <MedicineInfo :value="{ mc: record.sfxm?.mc, gg: record.sfxm?.gg, cj: record.sfxm?.cj }" />
            </template>
            <template v-if="column.dataIndex === 'yf'">
              {{ record.yf?.fyff }}
            </template>
            <template v-if="column.dataIndex === 'jlsl'"> {{ record.jlsl || 0 }} {{ record.sfxm?.jldw }} </template>
            <template v-if="column.dataIndex === 'cfts'"> {{ record.cfts }}天 </template>
            <template v-if="column.dataIndex === 'sl'"> {{ record.sl }}{{ record.sfxm?.dw }} </template>
            <template v-if="column.dataIndex === 'pl'">
              {{ record.pl?.pl }}
            </template>
          </template>
        </BaseTable>
      </div>
      <a-empty v-if="tableData.length === 0" />
    </div>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import BaseTable from '@/components/BaseTable'
import useDoctorStore from '../../store'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  dataSource: {
    type: Array,
    default: () => []
  },
  printType: {
    type: String,
    default: () => '0'
  }
})
const printParams = ref({})

const onCancel = () => {
  emit('update:open', false)
  tableData.value = []
  checkedData.value = []
}
const tableData = ref([])
const checkedData = ref([])
const doctorStore = useDoctorStore()

const getData = async () => {
  const { data, code } = await post('/outp/mzys/getPatientPrescriptionInfoList', { jzbh: doctorStore.currentPatient.jzbh, yzzt: 1 })
  if (code !== 0) message.error('获取数据失败')
  tableData.value = data || []
  if (tableData.value.length > 0) {
    tableData.value.forEach((item) => {
      item.checked = true
      item.expandedRowKeys = item.children.map((child) => child.id)
    })
    checkedData.value = tableData.value
  }
}
const onChangeCheck = () => {
  checkedData.value = tableData.value.filter((item) => {
    return item.checked
  })
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      checkedData.value = []
      getData()
    }
  }
)

const onOk = () => {
  checkedData.value.forEach(async (item, index) => {
    // const { code, msg, data } = await post('/outp/outPrint/getMedicinePrescriptionPrint', { cfbh: item.cfbh, jzbh: doctorStore.currentPatient.jzbh })
    if (props.printType === '0') {
      printParams.value = {
        cfbh: item.cfbh,
        jzbh: doctorStore.currentPatient.jzbh,
        printStatus: Number(props.printType),
        yzzt: 1
      }
    } else {
      printParams.value = {
        cfbh: item.cfbh,
        jzbh: doctorStore.currentPatient.jzbh,
        jfzt: Number(props.printType),
        yzzt: 1
      }
    }
    const { code, msg, data } = await post('/outp/outPrint/getMedicinePrescriptionOrdersPrint', printParams.value)
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data) //调用winform打印
    if (index === checkedData.value.length - 1) onCancel()
  })
}
</script>
<style lang="less" scoped>
:deep(.ant-table-row-expand-icon) {
  display: none;
}
</style>
